/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SaveGameDialog.java
 *
 * Created on Oct 12, 2010, 6:24:56 PM
 */

package net.worlds.component;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;
import net.worlds.Main;
import net.worlds.MainApplet;
import net.worlds.Util;
import net.worlds.ui.UI;

/**
 *
 * @author Jacob Morgan
 */
public class SaveGameDialog extends javax.swing.JFrame {

    /** Creates new form SaveGameDialog */
    public SaveGameDialog() {
        initComponents();
        setLocationRelativeTo(Main.getApplet());
        try
        {
            setIconImages(Util.icons);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        new Thread(new Runnable()
        {

            public void run() {
                String url;
                if(Main.clanid != 0)
                    url = "http://"+Main.siteURL+"/game/getgames.php?clanid="+Main.clanid;
                else
                    url = "http://"+Main.siteURL+"/game/getgames.php?user="+Main.getLoginID();
                final String result = Util.httpGet(url);
                SwingUtilities.invokeLater(new Runnable()
                {

                    public void run() {
                        String[] lines = result.split("\n");
                        if(lines[0].equals(""))
                            return;
                        for(String s : lines)
                        {
                            String[] parts = s.split("\\|");
                            int id = Integer.parseInt(parts[0]);
                            String name = parts[1];
                            ((DefaultListModel)gamesList.getModel()).addElement(new GameListRow(id, name));
                        }
                    }
                    
                });
            }

        }).start();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        listScroller = new javax.swing.JScrollPane();
        gamesList = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        statusTextArea = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Save to 2DWorlds.org");

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        gamesList.setModel(new DefaultListModel());
        listScroller.setViewportView(gamesList);

        jLabel1.setText("Select a game:");

        jLabel2.setText("Status:");

        statusTextArea.setColumns(20);
        statusTextArea.setFont(gamesList.getFont());
        statusTextArea.setRows(5);
        jScrollPane1.setViewportView(statusTextArea);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(okButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cancelButton))
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(listScroller, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(listScroller, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        if(gamesList.getSelectedIndex() == -1)
            return;
        Object el = ((DefaultListModel)gamesList.getModel()).getElementAt(gamesList.getSelectedIndex());
        final int gameID = ((GameListRow)el).id;
        Main.runInMainThread(new Runnable()
        {

            public void run() {
                statusTextArea.append("Starting upload...\n");
                try {
                    String r = UI.saveToURL(gameID, "http://"+Main.siteURL+"/game/save.php\n");
                    if(r.equals("OK"))
                        statusTextArea.append("Upload finised, click Cancel to exit or upload the game to another slot\n");
                    else
                        statusTextArea.append("An error occured: "+r);
                } catch (Exception ex) {
                    statusTextArea.append("Unexpected exception: "+ex+"\n");
                    ex.printStackTrace();
                }
            }
        });
    }//GEN-LAST:event_okButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SaveGameDialog().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JList gamesList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane listScroller;
    private javax.swing.JButton okButton;
    private javax.swing.JTextArea statusTextArea;
    // End of variables declaration//GEN-END:variables

}
